﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;

public partial class ShopOnlineAdmin_ProductThumbImage : SiteAdminBase
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        string productID = Request["ProductID"];
        if (!string.IsNullOrEmpty(productID))
        {
            FillList(productID);
        }
    }

    private void FillList(string productID)
    {
        DataTable table = new DataTable("Table");
        DataColumn column;
        DataColumnCollection columns = table.Columns;
        column = columns.Add("ForeignKey", typeof(System.String));
        column.MaxLength = 36;
        column = columns.Add("LoaclFileName", typeof(System.String));
        column.MaxLength = 254;
        column = columns.Add("FileName", typeof(System.String));
        column.MaxLength = 50;
        column = columns.Add("Url", typeof(System.String));
        column.MaxLength = 254;
        column = columns.Add("FullUrl", typeof(System.String));
        column.MaxLength = 254;
        column = columns.Add("Rect", typeof(System.String));
        column.MaxLength = 100;
        table.PrimaryKey = new DataColumn[] { table.Columns["ImageFileName"] };


        DataSet ds = ProductAccess.GetDataSet(productID);
        DataRow row = ds.Tables[0].Rows[0];
        DateTime dateTime = Convert.ToDateTime(row["CreateDate"].ToString());
        //string oldLocationID = documentCategoryAccess.GetStoreLocationID(row["CategoryID"].ToString());
        string localPath = StoreLocationAccess.CreateLocalResourceDirectory(row["StoreLocationID"].ToString(), dateTime, productID);
        string rootURL = StoreLocationAccess.GetResourceURL(row["StoreLocationID"].ToString(), dateTime, productID);
        string[] files = Directory.GetFiles(localPath);        
        foreach (string file in files)
        {
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(file);
            FileInfo fileInfo = new FileInfo(file);
            DataRow newRow = table.NewRow();
            newRow["ForeignKey"] = productID;
            newRow["LoaclFileName"] = file;
            newRow["FileName"] = fileInfo.Name;
            newRow["Url"] = "../" + rootURL + "/" + fileInfo.Name;
            newRow["FullUrl"] = Request.Url.Scheme + "://" + Request.Url.Authority + base.ApplicationPath + "/" + rootURL + "/" + fileInfo.Name;
            newRow["Rect"] = image.Width.ToString() + " x " + image.Height.ToString();
            image.Dispose();
            table.Rows.Add(newRow);
        }

        //排序
        DataTable newTable = table.Clone();
        foreach (DataRow eachRow in table.Select("", "FileName"))
        {
            newTable.ImportRow(eachRow);
        }
        dataList.DataSource = newTable;
        dataList.DataBind();
    }
}